package io.renren.sf.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.sf.entity.CbBookEditionEntity;
import io.renren.sf.entity.CbChapterEntity;
import io.renren.sf.entity.CbContentsEntity;
import io.renren.sf.service.CbContentsService;
import io.renren.sf.vo.ChapterVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.sf.entity.CbUnitEntity;
import io.renren.sf.service.CbUnitService;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author cyl_txw
 * @email cyl_txw@gmail.com
 * @date 2021-04-06 13:32:31
 */
@RestController
@RequestMapping("sf/cbunit")
public class CbUnitController {
    @Autowired
    private CbUnitService cbUnitService;
    @Autowired
    private CbContentsService cbContentsService;
    /**
     * 通过id获取contentsId
     */
    @GetMapping("/getContentsIdById/{id}")
    public R getContentsIdById(@PathVariable("id") Long id){
        // 先看看是不是contentsId
        CbContentsEntity byId = cbContentsService.getById(id);
        if(byId != null){
            // 说明是contentsId
            return R.ok().put("contentsId", id);
        }else {
            // 是unitId，通过它获取contentsId
            Long contentsId = cbUnitService.getById(id).getCid();
            return R.ok().put("contentsId", contentsId);
        }
    }

    /**
     * 单元分页
     */
    @GetMapping("/list")
    public R chapterPage(@RequestParam Map<String, Object> params) {
        Page<CbUnitEntity> unitPage =  cbUnitService.getUnitByBookId(params); // 是contentsId
        return R.ok().put("unitPage", unitPage);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("sf:cbunit:info")
    public R info(@PathVariable("id") Long id){
		CbUnitEntity cbUnit = cbUnitService.getById(id);

        return R.ok().put("cbUnit", cbUnit);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("sf:cbunit:save")
    public R save(@RequestBody CbUnitEntity cbUnit){
		cbUnitService.save(cbUnit);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("sf:cbunit:update")
    public R update(@RequestBody CbUnitEntity cbUnit){
		cbUnitService.updateById(cbUnit);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("sf:cbunit:delete")
    public R delete(@RequestBody Long[] ids){
		cbUnitService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
